﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using CoolERP_Common;

namespace CoolERP_Web.User
{
    public partial class CGD_TO_YFKZD : BasePage
    {
        private Guid CGDID
        {
            get { return this.GetUrlParam("id") == null ? Guid.Empty : new Guid(this.GetUrlParam("id").ToString()); }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDL();
                BindData();
            }
        }
        
        //初始化下拉框
        private void BindDDL()
        {
            //初始化结算方式下拉框
            using (CS_JSFSBLL cB = new CS_JSFSBLL())
            {
                this.ddlJSFS.DataSource = cB.GetAll();
                this.ddlJSFS.DataValueField = "BH";
                this.ddlJSFS.DataTextField = "MC";
                this.ddlJSFS.DataBind();
                this.ddlJSFS.Items.Insert(0, "请选择");
                this.ddlJSFS.SelectedIndex = 0;
            }
        }
        private void BindData()
        {
            string cgdBH = this.GetUrlParam("bh").ToString();
            string gysJC = this.GetUrlParam("gysjc").ToString();
            if (CGDID != Guid.Empty)
            {
                if (!string.IsNullOrEmpty(cgdBH))
                    this.tbCGDBH.Text = cgdBH.Trim();
                if (!string.IsNullOrEmpty(gysJC))
                    this.tbGYSJC.Text = gysJC.Trim();
                using (CGDBLL cgdB = new CGDBLL())
                {
                    CGD entityCGD = cgdB.GetEntity(t => t.ID == CGDID);
                    this.tbRQ.Text = entityCGD.RQ.ToString().Trim();
                    this.tbDHSJ.Text = entityCGD.DHSJ.Value.ToString().Trim();
                }
                using (CGDMXBBLL cgdmxB = new CGDMXBBLL())
                {
                    double sum = Convert.ToDouble(cgdmxB.GetList(t => t.CGDID == CGDID).Sum(t => t.DJ * t.SL));
                    this.tbYFZE.Text = sum.ToString();
                }
            }
            else return;
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            bool isAdd = true;
            bool result = false;
            if (string.IsNullOrEmpty(tbDZSJ.Text.Trim()))
            {
                ShowMessage("对帐时间为必填项，请确认无误后再保存！");
                this.tbDZSJ.Focus();
                return;
            }
            if (string.IsNullOrEmpty(tbJQSJ.Text.Trim()))
            {
                ShowMessage("结清时间为必填项，请确认无误后再保存！");
                this.tbJQSJ.Focus();
                return;
            }

            if (Convert.ToInt32(ddlJSFS.SelectedIndex) == 0)
            {
                ShowMessage("请选择结算方式");
                this.ddlJSFS.Focus();
                return;
            }
            using (CW_YFKZDBLL yfkzdB = new CW_YFKZDBLL())
            {
                CW_YFKZD entity = new CW_YFKZD();
                if (CGDID != Guid.Empty)
                {
                    CGDBLL cgdB = new CGDBLL();
                    CGD cgd = cgdB.GetEntity(t => t.ID == CGDID);
                    entity.ID = Guid.NewGuid();
                    ZL_YWLSH ywItem = new ZL_YWLSH();
                    entity.BH = GenYWLSH.GenBH(EnumHelper.GetDescriptionByName(typeof(EnumYWDJLX), EnumYWDJLX.YFKZD), out ywItem, out isAdd);
                    entity.CGDID = CGDID;
                    entity.CZRYID = this.currentUser.UserID;
                    entity.CJSJ = DateTime.Now;
                    entity.DZSJ = DateTime.Parse(this.tbDZSJ.Text.Trim());
                    entity.JQSJ = DateTime.Parse(this.tbJQSJ.Text.Trim());
                    entity.YFZE = Convert.ToDouble(this.tbYFZE.Text.Trim());
                    entity.ZDZT = 0;
                    entity.YIFUZE = 0;
                    entity.JSFSBH = ddlJSFS.SelectedValue.ToString().Trim();
                }
                try
                {
                    result = yfkzdB.Insert(entity);
                }
                catch (Exception ex)
                {
                    ShowMessage(ex.Message);
                }
            }
            if (result)
            {
                lbResult.Text = "保存成功";
            }
            else lbResult.Text = "保存失败";
        }
    }
}